package com.example.rk.service.impl;
import com.example.rk.common.MyPageInfo;
import com.example.rk.entity.RenKo;
import com.example.rk.mapper.RenKoMapper;
import com.example.rk.service.RenKoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class RenKoServiceImpl implements  RenKoService{
@Autowired
    private RenKoMapper mapper;

//    @Override
//    public List<RenKo> findAll() {
////        return mapper.findAll();
//    }

    @Override
    public void deleteById(int id) {
mapper.deleteById(id);
    }

    @Override
    public String doAdd(RenKo renko) {
        RenKo dbrenko=mapper.findadd(renko.getDiqu(),renko.getNumber());
        if (dbrenko==null){
            mapper.create(renko);
            return "ok";
        }else{
            return "exist";
        }

    }

    @Override
    public RenKo findById(int id) {
        return mapper.findByID(id);
    }

    @Override
    public String doEdit(RenKo renko) {
        try {
            mapper.doEdit(renko);
            return "ok";
        }catch (Exception e){
            System.out.println("RenKoServiceImpl.doEdit() ERROR:e="+e);
        }
        return "error";

    }
    @Override
    public MyPageInfo findBySearch(HttpServletRequest request) {
        int currentPageNum=1;  //当前页
        int pageSize=3;  //每页大小
        String currentPageNumStr = request.getParameter("pageNum");
        if (currentPageNumStr!=null){
            currentPageNum=Integer.parseInt(currentPageNumStr);
        }


        String data = request.getParameter("data");
        String diqu = request.getParameter("diqu");

//        int count=mapper.count(code,name);
//        int totalPageNum=count/pageSize + count%pageSize ;  //总页码数

        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在执行sql之前执行
        mapper.findBySearch(data,diqu);
        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),
                pageInfo.getNavigateLastPage(),pageInfo.getTotal(),pageInfo.getList());


        myPageInfo.setSearch1(data);
        myPageInfo.setSearch2(diqu);

        return myPageInfo;
//        return mapper.findBySearchWithPage(code,name,(currentPageNum-1)*3,pageSize);
    }
}

